package net.java.mediasearch.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import net.java.mediasearch.exception.OfdbMediaSearchContentException;
import net.java.mediasearch.model.MediaEntity;
import net.java.mediasearch.model.SmallMediaEntity;

/**
 * ResponseXMLParser.
 */
public class ResponseXMLParser {

	public List<SmallMediaEntity> parseXmlResponseContaintingMediaEntityList(InputStream inputStream) throws OfdbMediaSearchContentException {
		SAXBuilder builder = new SAXBuilder();
		
		try {
			Document document = builder.build(inputStream);
			
			Element rootElement = document.getRootElement();
			
			validateXmlResult(rootElement);
			
			Element resultElement = rootElement.getChild("resultat");
			
			List<Element> entryElements = resultElement.getChildren("eintrag");
			
			List<SmallMediaEntity> smallMediaEntities = new ArrayList<SmallMediaEntity>();
			
			for (Element curEntryElement : entryElements) {
				String id = curEntryElement.getChildTextTrim("id");
				String title = curEntryElement.getChildTextTrim("titel");
				int year = Integer.parseInt(curEntryElement.getChildTextTrim("jahr"));
				
				smallMediaEntities.add(new SmallMediaEntity(id, title, year)); 
			}
			
			return smallMediaEntities;
		} catch (OfdbMediaSearchContentException ofdbMediaSearchContentException) {
			throw ofdbMediaSearchContentException;
		} catch(Exception generalException) {
			throw new OfdbMediaSearchContentException(OfdbMediaSearchContentException.RESULTCODE_XML_PARSE_ERROR, 
					"Error parsing the XML-Document", generalException);
		}
	}
	
	private void validateXmlResult(Element rootElement) throws OfdbMediaSearchContentException {
		int resultCode = parseResultCode(rootElement);
		if (resultCode != 0) {
			throw new OfdbMediaSearchContentException(resultCode);
		}
	}
	
	private int parseResultCode(Element rootElement) {
		Element statusElement = rootElement.getChild("status");
		Element resultCodeElement = statusElement.getChild("rcode");
		String resultCodeString = resultCodeElement.getTextTrim();
		return Integer.parseInt(resultCodeString);
	}
	
	public MediaEntity parseXmlResponseContaintingMediaEntiyDetail(InputStream inputStream) throws OfdbMediaSearchContentException {
		SAXBuilder builder = new SAXBuilder();
		
		try {
			Document document = builder.build(inputStream);
			
			Element rootElement = document.getRootElement();
			
			validateXmlResult(rootElement);
			
			Element resultElement = rootElement.getChild("resultat");
			
			String title = resultElement.getChildTextTrim("titel");
			int year = Integer.parseInt(resultElement.getChildTextTrim("jahr"));
			String pictureUrl = resultElement.getChildTextTrim("bild");
			String pictureName = title + "." + getPictureContentType(pictureUrl);
			byte[] pictureBytes = PictureUtil.getPictureForUrl(pictureUrl);
			String description = resultElement.getChildTextTrim("beschreibung");
			Element genreElement = resultElement.getChild("genre");
			List<Element> genreTitleElements = genreElement.getChildren("titel");
			
			List<String> genres = new ArrayList<String>();
			for(Element curGenreTitleElement : genreTitleElements) {
				genres.add(curGenreTitleElement.getTextTrim());
			}
			
			MediaEntity mediaEntity = new MediaEntity();
			mediaEntity.setDescription(description);
			mediaEntity.setGenres(genres);
			mediaEntity.setPictureBytes(pictureBytes);
			mediaEntity.setPictureName(pictureName);
			mediaEntity.setReleaseYear(year);
			mediaEntity.setTitle(title);
			
			return mediaEntity;
			
		} catch (OfdbMediaSearchContentException ofdbMediaSearchContentException) {
			throw ofdbMediaSearchContentException;
		} catch(Exception generalException) {
			throw new OfdbMediaSearchContentException(OfdbMediaSearchContentException.RESULTCODE_XML_PARSE_ERROR, 
					"Error parsing the XML-Document", generalException);
		}
	}
	
	private String getPictureContentType(String pictureUrl) {
		int startIndex = pictureUrl.lastIndexOf(".") + 1;
		
		return pictureUrl.substring(startIndex);
		
	}
}
